package com.whw.fy.controller;

import com.whw.fy.entity.Message;
import com.whw.fy.entity.Room;
import com.whw.fy.entity.Schedule;
import com.whw.fy.service.ScheduleService;
import com.whw.fy.utils.GetEveryDate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Api(tags = "排课记录模块")
@RestController
@RequestMapping("/api/schedule")
public class ScheduleController {
    @Resource
    private ScheduleService scheduleService;

    @ApiOperation(value = "新增排课")
    @PostMapping("/")
    public Message insert(Schedule s) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (s.getTimeDaytoString()!=null){
            s.setTimeDay(simpleDateFormat.parse(s.getTimeDaytoString()));
            int weekname =GetEveryDate.GetWeekByDate(s.getTimeDaytoString());
            s.setWeekName(weekname);
            if (weekname==6||weekname==7)
                s.setScheduleWorkOvertime(1);
            else {
                //第十节课是晚上5点开始，算加班
                if (s.getTimeSkcd()>=9)
                    s.setScheduleWorkOvertime(1);
                else {
                    s.setScheduleWorkOvertime(0);
                }
            }
        }
        return scheduleService.insertSelective(s);
    }

    @ApiOperation(value = "根据周几批量增加排课")
    @PostMapping("/all/accordingToTheDayOfTheWeek")
    public Message batchInsertAccordingToTheDayOfTheWeek(Schedule schedule,int times,String begindate) throws ParseException {
        return scheduleService.batchInsertAccordingToTheDayOfTheWeek(schedule,times,begindate);
    }


    @ApiOperation(value = "根据id删除排课")
    @DeleteMapping("/")
    public Message delete(int id){
        return scheduleService.deleteByPrimaryKey(id);
    }

    @ApiOperation(value = "修改排课")
    @PutMapping("/")
    public Message update(Schedule s) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (s.getTimeDaytoString()!=null){
            s.setTimeDay(simpleDateFormat.parse(s.getTimeDaytoString()));
            int weekname =GetEveryDate.GetWeekByDate(s.getTimeDaytoString());
            s.setWeekName(weekname);
            if (weekname==6||weekname==7)
                s.setScheduleWorkOvertime(1);
            else {
                //第十节课是晚上5点开始，算加班
                if (s.getTimeSkcd()>=9)
                    s.setScheduleWorkOvertime(1);
                else {
                    s.setScheduleWorkOvertime(0);
                }
            }
        }
        return scheduleService.updateByPrimaryKeySelective(s);
    }

    @ApiOperation(value = "根据id查看排课")
    @GetMapping("/one")
    public Message selectById(int id){
        return scheduleService.selectByPrimaryKey(id);
    }

    @ApiOperation(value = "查看所有排课")
    @GetMapping("/all")
    public Message selectALL(){
        return scheduleService.selectALL();
    }

    @ApiOperation(value = "根据老师id查询当天排课记录")
    @GetMapping("/TeacherId")
    public Message selectTodayScheduleByTeacherId(int TeacherId){
        return scheduleService.selectTodayScheduleByTeacherId(TeacherId);
    }
    @ApiOperation(value = "小程序根据学生id获得学生当天的课程表")
    @GetMapping("/applet/StudentId")
    public Message selectTodayScheduleByStudentId(int studentId){
        return scheduleService.selectTodayScheduleByStudentId(studentId);
    }


    @ApiOperation(value = "小程序根据老师id获得本周课程表")
    @GetMapping("/applet/teacherID")
    public Message selectappletByTeacherId(int teacherId){
        return scheduleService.selectappletByTeacherId(teacherId);
    }

    @ApiOperation(value = "小程序根据学生id获得本周课程表")
    @GetMapping("/applet/studentID")
    public Message appletselectappletByStudentId(int studentId){
        return scheduleService.appletselectappletByStudentId(studentId);
    }

    @ApiOperation(value = "根据学生id获得本周课程表")
    @GetMapping("/all/studentID")
    public Message selectappletByStudentId(int studentId){
        return scheduleService.selectappletByStudentId(studentId);
    }

    @ApiOperation(value = "小程序根据老师id和classId获得本周课程表")
    @GetMapping("/applet/teacherIdAndClassId")
    public Message selectappletByTeacherIdAndClassId(int teacherId,int classId){
        return scheduleService.selectappletByTeacherIdAndClassId(teacherId,classId);
    }

    @ApiOperation(value = "小程序获取默认课程表")
    @GetMapping("/applet")
    public Message selectApplet(){
        return scheduleService.selectApplet();
    }

    @ApiOperation(value = "根据排课id和当前时间判断课程状态")
    @GetMapping("/selectClassStatusByScheduleId")
    public Message selectClassStatusByScheduleId(int id)throws ParseException{
        return scheduleService.selectClassStatusByScheduleId(id);
    }

    @ApiOperation(value = "管理端根据班级id和时间获得所在周的课表")
    @GetMapping("/selectByClazzIdAndTime")
    public Message selectByClazzIdAndTime(int clazzId,String Time) throws ParseException{
        return scheduleService.selectByClazzIdAndTime(clazzId, Time);
    }

    @ApiOperation(value = "管理端根据学生id获得所在周的课表")
    @GetMapping("/selectByStudentId")
    public Message selectByStudentId(int studentId) throws ParseException{
        return scheduleService.selectByStudentId(studentId);
    }
    @ApiOperation(value = "根据班级id获得全部排课记录")
    @GetMapping("/all/clasid")
    public Message selectByClassId(int classId){
        return scheduleService.selectByClassId(classId);
    }

    @ApiOperation(value = "查询该时间段可上课的老师")
    @GetMapping("/checking/teacher")
    public Message cheakingTeacher(Date day,int timeSkcd,int timeSkjc) throws ParseException {
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        Date day = simpleDateFormat.parse(Stringday);
        return scheduleService.cheakingTeacher(day, timeSkcd,timeSkjc);
    }

    @ApiOperation(value = "查询该时间段内可使用的教室")
    @GetMapping("/checking/room")
    public Message cheakingRoom(Date day,int timeSkcd,int timeSkjc) throws ParseException {
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        Date day = simpleDateFormat.parse(Stringday);
        return scheduleService.cheakingRoom(day,timeSkcd,timeSkjc);
    }

    @ApiOperation(value = "修改排课状态测试别连")
    @GetMapping("/TEST")
    public Message test(int classId){
        return scheduleService.test(classId);
    }

    @ApiOperation(value = "管理端获得当天场地排课表")
    @GetMapping("/roomscheduleOfTheDayInDay")
    public Message roomScheduleOfTheDayInDay(){
        return scheduleService.roomScheduleOfTheDayInDay();
    }

    @ApiOperation(value = "管理端获得当天教师排课表")
    @GetMapping("/scheduleOfTheDayInDayByTeacher")
    public Message teacherSchedule(){
        return scheduleService.teacherSchedule();
    }

    @ApiOperation(value = "管理端获得当天资源排课表")
    @GetMapping("/resourcesScheduleOfTheDayInDay")
    public Message resourcesScheduleOfTheDayInDay(){
        return scheduleService.resourcesScheduleOfTheDayInDay();
    }
}
